/*Copyright (C) 2013-2020 LZE*/
#include<bits/stdc++.h>
#define INF 0x7f7f7f7f
using namespace std;
typedef long long ll;
int n,a[1000000]={INF},ans[1000000],gen=0;
inline int rd() {
	int x=0;bool ntv=false;char c=getchar();
	while((c<'0'||c>'9')&&c!='-')c=getchar();if(c=='-')ntv=true;else x=(c^48);c=getchar();
	while(c>='0'&&c<='9') {x=x*10+(c^48);c=getchar();}return ntv?-x:x;
}
int main() {
	n=rd();
	for(int i=1;i<=n;i++)
	{
		a[i]=rd();
		int self=i;
		while(a[self/2]<a[self]&&self>1){
			swap(a[self/2],a[self]);
			self/=2;
		}
	}
	for(int i=1;i<=n;i++){
		ans[i]=a[1];
		swap(a[1],a[n-i+1]);
		a[n-i+1]=0;
		int self=1;
		while(self<=n-i+1){
			if(a[self]>a[self*2]&&a[self]>a[self*2+1])break;
			if(a[self*2]>=a[self*2+1]){
				swap(a[self],a[self*2]);
				self*=2;
			}else{
				swap(a[self],a[self*2+1]);
				self=self*2+1;
			}
		}
	}
	for(int i=1;i<=n;i++)cout<<ans[n-i+1]<<" ";
	return 0;
}
